// router/index.js
import { createRouter, createWebHistory } from 'vue-router'
import TokenStorage from "../utils/token.js";

// 管理端组件
import DataAnalysis from '../views/admin/DataAnalysis.vue'
import AnnouncementManage from '../views/admin/AnnouncementManage.vue'
import InsectInfoManage from '../views/admin/InsectInfoManage.vue'
import InsectCategoryManage from '../views/admin/InsectCategoryManage.vue'
import CarouselManage from '../views/admin/CarouselManage.vue'
import PostManage from '../views/admin/PostManage.vue'
import RoleManage from '../views/admin/RoleManage.vue'
import RecognitionManage from '../views/admin/RecognitionManage.vue'
import UserManage from '../views/admin/UserManage.vue'
import AdminHome from "../views/admin/AdminHome.vue";

// 用户端组件
import PersonalInfo from '../views/user/PersonalInfo.vue'
import InsectRecognition from '../views/user/InsectRecognition.vue'
import KnowledgeBase from '../views/user/KnowledgeBase.vue'
import FeedbackSubmit from '../views/user/FeedbackSubmit.vue'
import PostSharing from '../views/user/PostSharing.vue'

// 公共组件
import Main from '../views/Main.vue'
import Home from '../components/Home.vue'
import Login from '../views/Login.vue'
import Register from '../views/Register.vue'

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/admin',
    name: 'admin',
    component: Main,
    children: [
      { path: 'dashboard', name: 'admin-dashboard', component: AdminHome },
      { path: 'analysis', name: 'admin-analysis', component: DataAnalysis },
      { path: 'announcement', name: 'admin-announcement', component: AnnouncementManage },
      { path: 'insect-info', name: 'admin-insect-info', component: InsectInfoManage },
      { path: 'insect-category', name: 'admin-insect-category', component: InsectCategoryManage },
      { path: 'carousel', name: 'admin-carousel', component: CarouselManage },
      { path: 'posts', name: 'admin-posts', component: PostManage },
      { path: 'roles', name: 'admin-roles', component: RoleManage },
      { path: 'recognition', name: 'admin-recognition', component: RecognitionManage },
      { path: 'users', name: 'admin-users', component: UserManage }
    ]
  },
  {
    path: '/user',
    name: 'user',
    component: Main,
    children: [
      { path: 'home', name: 'user-home', component: Home },
      { path: 'profile', name: 'user-profile', component: PersonalInfo },
      { path: 'recognize', name: 'user-recognize', component: InsectRecognition },
      { path: 'knowledge', name: 'user-knowledge', component: KnowledgeBase },
      { path: 'feedback', name: 'user-feedback', component: FeedbackSubmit },
      { path: 'community', name: 'user-community', component: PostSharing }
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: Login
  },
  {
    path: '/register',
    name: 'register',
    component: Register
  }
];

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes
});

// 导航守卫
router.beforeEach((to, from, next) => {
  const token = TokenStorage.getToken();
  const user = TokenStorage.get();

  // 公共路由
  const publicRoutes = ['login', 'register'];

  // 1. 未认证处理
  if (!token) {
    return publicRoutes.includes(to.name)
        ? next()
        : next({
          name: 'login',
          query: { redirect: to.fullPath }
        });
  }

  // 2. 已认证用户访问公共页面
  if (publicRoutes.includes(to.name)) {
    return next(user.role === 'admin' ? '/admin/dashboard' : '/user/home');
  }

  // 3. 角色权限验证
  if (to.name.startsWith('admin-') && user.role !== 'admin') {
    return next('/user/home');
  }

  if (to.name.startsWith('user-') && user.role !== 'user') {
    return next('/admin/dashboard');
  }

  // 4. 默认放行
  next();
});

export default router;
